/**
 * 文件名：AbstractProcess.java
 *
 * 创建人：Elvin Zhao - 279152260@qq.com
 *
 * 创建时间：2019年1月2日 下午3:17:12
 *
 * 版权所有：FNII
 */
package com.z100h.supplychain.core.process;

import java.util.List;

import org.apache.commons.collections.ListUtils;
import org.springframework.beans.factory.annotation.Autowired;

import com.jeeplus.core.facade.BaseFacade;
import com.z100h.supplychain.core.EvidenceLockState;
import com.z100h.supplychain.core.EvidenceState;
import com.z100h.supplychain.core.ResourceLockState;
import com.z100h.supplychain.core.ResourceStockoutLockState;
import com.z100h.supplychain.core.biz.service.CommonEvidenceService;
import com.z100h.supplychain.core.biz.service.ResourceLogService;
import com.z100h.supplychain.core.biz.service.ResourceService;
import com.z100h.supplychain.evidence.entity.StockoutSku;
import com.z100h.supplychain.warehouse.entity.Warehouse.WarehouseType;

/**
 * 
 * [描述信息：说明类的基本功能]
 *
 * @author Elvin Zhao - 279152260@qq.com
 * @version 1.0 Created on 2019年1月2日 下午7:24:18
 */
public class BaseProcess extends BaseFacade {

	@Autowired
	protected CommonEvidenceService commonEvidenceService;

	@Autowired
	protected ResourceService resourceService;

	@Autowired
	protected ResourceLogService resourceLogService;

	/**
	 * 
	 * 检查资源凭据锁和出库锁锁定状态
	 * 
	 * @param resourceIds
	 * @param evidenceId
	 * @param evidenceLockState
	 * @param resourceStockoutLockState
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月26日 上午11:24:07
	 */
	@SuppressWarnings("unchecked")
	protected List<String> checkResource(final List<String> resourceIds, String evidenceId,
			EvidenceLockState evidenceLockState, ResourceStockoutLockState resourceStockoutLockState) {
		List<String> correctList = resourceService.checkResource(evidenceId, evidenceLockState,
				resourceStockoutLockState, resourceIds, null);
		return ListUtils.removeAll(resourceIds, correctList);
	}

	/**
	 * 
	 * 检查资源凭据锁和出库锁锁定状态
	 * 
	 * @param resourceIds
	 * @param evidenceLockState
	 * @param resourceStockoutLockState
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月17日 下午3:55:10
	 */
	@SuppressWarnings("unchecked")
	protected List<String> checkResource(final List<String> resourceIds, EvidenceLockState evidenceLockState,
			ResourceStockoutLockState resourceStockoutLockState) {
		List<String> correctList = resourceService.checkResource(null, evidenceLockState, resourceStockoutLockState,
				resourceIds, null);
		return ListUtils.removeAll(resourceIds, correctList);
	}

	/**
	 * 
	 * 检查资源凭据锁和出库锁锁定状态和仓库类型
	 * 
	 * @param resourceIds
	 * @param evidenceLockState
	 * @param resourceStockoutLockState
	 * @param warehouseType
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月19日 下午3:15:47
	 */
	@SuppressWarnings("unchecked")
	protected List<String> checkResource(final List<String> resourceIds, EvidenceLockState evidenceLockState,
			ResourceStockoutLockState resourceStockoutLockState, WarehouseType warehouseType) {

		List<String> correctList = resourceService.checkResource(null, evidenceLockState, resourceStockoutLockState,
				resourceIds, warehouseType);
		return ListUtils.removeAll(resourceIds, correctList);
	}

	/**
	 * 
	 * 根据凭证id 检查资源凭据锁和出库锁锁定状态
	 * 
	 * @param evidenceId
	 * @param evidenceLockState
	 * @param resourceStockoutLockState
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月17日 下午3:56:19
	 */
	@SuppressWarnings("unchecked")
	protected List<String> checkResource(final String evidenceId, final EvidenceLockState evidenceLockState,
			ResourceStockoutLockState resourceStockoutLockState) {
		List<String> resourceIds = resourceService.findIdByEid(evidenceId);
		List<String> correctList = resourceService.checkResource(evidenceId, evidenceLockState,
				resourceStockoutLockState, null, null);
		return ListUtils.removeAll(resourceIds, correctList);
	}

	/**
	 * 
	 * 锁定资源
	 * 
	 * @param resourceIds
	 * @param evidenceLockState
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:22:23
	 */
	protected Boolean lockResource(final List<String> resourceIds, final EvidenceLockState evidenceLockState) {
		try {
			resourceService.lockResource(resourceIds, evidenceLockState);
			return Boolean.TRUE;
		} catch (Exception e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
	}

	/**
	 * 
	 * 锁定资源
	 * 
	 * @param evidenceId
	 * @param evidenceLockState
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:21:57
	 */
	protected Boolean lockResource(final String evidenceId, final EvidenceLockState evidenceLockState) {
		try {
			resourceService.lockResource(evidenceId, evidenceLockState);
			return Boolean.TRUE;
		} catch (Exception e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
	}

	/**
	 * 
	 * 关联 单据 与 资源
	 * 
	 * @param evidenceId
	 * @param resourceIds
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:21:28
	 */
	protected void associateEvidenceWithResource(final String evidenceId, final List<String> resourceIds) {
		resourceService.associateEvidenceWithResource(evidenceId, resourceIds);
	}

	/**
	 * 
	 * 完成 单据
	 * 
	 * @param evidenceId
	 * @param evidenceState
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:21:09
	 */
	protected void finishEvidence(final String evidenceId, final EvidenceState evidenceState) {
		commonEvidenceService.finishEvidence(evidenceId, evidenceState);
	}

	/**
	 * 
	 * 解锁资源
	 * 
	 * @param resourceIds
	 * @param evidenceLockState
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:20:38
	 */
	protected Boolean unlockResource(final List<String> resourceIds, final EvidenceLockState evidenceLockState) {
		try {
			resourceService.unlockResource(resourceIds, evidenceLockState, null);
			return Boolean.TRUE;
		} catch (Exception e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
	}

	/**
	 * 
	 * 解锁资源
	 * 
	 * @param resourceIds
	 * @param evidenceLockState
	 * @return
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:20:38
	 */
	protected Boolean unlockResourceByEid(final String evidenceId, final EvidenceLockState evidenceLockState) {
		try {
			resourceService.unlockResourceByEid(evidenceId, evidenceLockState, null);
			return Boolean.TRUE;
		} catch (Exception e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
	}

	/**
	 * 
	 * 解锁资源（凭据锁和出库锁）
	 * 
	 * @param resourceIds
	 * @param evidenceLockState
	 * @param resourceStockoutLockState
	 * @return
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年1月26日 下午3:12:48
	 */
	protected Boolean unlockResource(final List<String> resourceIds, final EvidenceLockState evidenceLockState,
			final ResourceStockoutLockState resourceStockoutLockState) {
		try {
			resourceService.unlockResource(resourceIds, evidenceLockState, resourceStockoutLockState);
			return Boolean.TRUE;
		} catch (Exception e) {
			e.printStackTrace();
			return Boolean.FALSE;
		}
	}

	/**
	 * 
	 * 资源日志记录
	 * 
	 * @param evidenceId
	 * @param evidenceLockState
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年1月8日 下午4:20:55
	 */
	protected void resourceLogging(final String evidenceId, final ResourceLockState evidenceLockState) {
		resourceLogService.resourceLogging(evidenceId, evidenceLockState);
	}

	/**
	 * 
	 * 出库资源日志记录
	 * 
	 * @param evidenceId
	 * @param evidenceLockState
	 * @param resourceIds
	 * @author: Fyh - 1009173473@qq.com 
	 * @date: Created on 2019年3月19日 下午5:17:39
	 */
	protected void resourceLogging(final String evidenceId, final ResourceLockState evidenceLockState,
			List<String> resourceIds) {
		resourceLogService.resourceLogging(evidenceId, evidenceLockState, resourceIds);
	}

	/**
	 * 出库资源日志记录
	 * evidenceId,skuId
	 * 
	 * @param stockoutSku
	 * @param evidenceLockState
	 * @author: Elvin Zhao - 279152260@qq.com 
	 * @date: Created on 2019年5月14日 下午5:05:10
	 */
	protected void resourceLogging(final StockoutSku stockoutSku, final ResourceLockState resourceLockState) {
		resourceLogService.stockoutResourceLoggingBySkuIdAndEid(stockoutSku.getEvidenceId(),
				resourceLockState.getValue(), stockoutSku.getSkuId());
	}

	protected void resourceLogging(final String erpResId, final String evidenceId,
			final ResourceLockState resourceLockState) {
		resourceLogService.stockoutResourceLoggingByErpSku(erpResId, evidenceId, resourceLockState.getValue());
	}
}
